#include <stdio.h>
#include <iostream>
#include <fstream>
class CCsvWriter {
  private:
    FILE* mainfile;
    char* path;
    int current_linenum;
    int limit_linenum;
  public:
    CCsvWriter(char* csvpath,char* module) {
        path = csvpath;
        mainfile = fopen(csvpath, module);
        current_linenum = 0;
        limit_linenum=10;
    }
    void saveTemp() {
        fclose(mainfile);
        mainfile = fopen(path, "a+");
    }
    void write_row(char* line) { 
        fputs(line, mainfile);
        ++current_linenum;
        if(current_linenum >= limit_linenum){
            current_linenum=0;
            saveTemp();
        }
    }
    void write_line(char* buffer) {
        fputs(buffer, mainfile);
        fputc('\n', mainfile);
        ++current_linenum;
        if(current_linenum >= limit_linenum){
            current_linenum=0;
            saveTemp();
        }
    }
    void write_char(char c) { fputc(c, mainfile); }
    ~CCsvWriter() { fclose(mainfile); }
};
class StreamCsvWriter {
private:
    std::ofstream ofs;
    std::string path;
    int my_mb;
public:
    StreamCsvWriter(std::string input) {
        ofs.open(input, std::ios::app);
        path = input;
        my_mb = 2;
    }
    ~StreamCsvWriter() {
        ofs.close();
    }
    //mb=1表示使用正常写 2:表示追加
    void changeMod(int mb) {
        if (mb == 1) {
            ofs.close();
            ofs.open(path, std::ios::out);
        }
        else {
            ofs.close();
            ofs.open(path, std::ios::app);
        }
    }
    void write_row(char* row) {
        ofs << row << std::endl;
    }
    void Save_current() {
        ofs.close();
        if (my_mb == 1) {
            ofs.open(path, std::ios::out);
        }
        else {
            ofs.open(path, std::ios::app);
        }
    }
};